﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Interfleet.SE.UICompiler.Core.Batch;
using Interfleet.SE.UICompiler.Core.ClosureCompiler;
using Moq;
using Interfleet.SE.UICompiler.Core;

namespace Interfleet.SE.UICompiler.Tests.Batch
{
    /// <summary>
    /// Summary description for BatchJobPersistorTests
    /// </summary>
    [TestClass]
    public class BatchJobPersistorTests
    {
        public BatchJobPersistorTests()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void BatchPersistor_Constructor_When_BatchFileIsNull_Then_ArgumentNullExceptionIsThrown()
        {
            new BatchPersistor(null, new CCArguments());
            Assert.Fail();
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void BatchPersistor_Constructor_When_ArgumentsIsNull_Then_ArgumentNullExceptionIsThrown()
        {
            new BatchPersistor("test", null);
            Assert.Fail();
        }

        [TestMethod]
        [ExpectedException(typeof(ArgumentNullException))]
        public void BatchPersistor_Constructor_When_FileProviderIsNull_Then_ArgumentNullExceptionIsThrown()
        {
            new BatchPersistor("test", new CCArguments(), null);
            Assert.Fail();
        }

        [TestMethod]
        public void BatchPersistor_Constructor_VerifyXmlIsNotNull()
        {
            var args = new CCArguments();
            args.CompileOption = "SIMPLE_OPTIMIZATIONS";
            args.CompiledFileName = @"c:\output.js";
            args.Files.Add(@"c:\input.js");
            var fileMock = new Mock<IFileProvider>();
            fileMock.Setup(f => f.SaveToFile(It.IsAny<string>(), It.IsAny<string>()));
            var persistor = new BatchPersistor("test", args, fileMock.Object);
            var pi = new PrivateObject(persistor);
            var accessor = new BatchPersistor_Accessor(pi);

            Assert.IsNotNull(accessor._xElement);

        }

        [TestMethod]
        public void BatchPersistor_Constructor_VerifyXmlRootNameIsBatch()
        {
            var args = new CCArguments();
            args.CompileOption = "SIMPLE_OPTIMIZATIONS";
            args.CompiledFileName = @"c:\output.js";
            args.Files.Add(@"c:\input.js");
            var fileMock = new Mock<IFileProvider>();
            fileMock.Setup(f => f.SaveToFile(It.IsAny<string>(), It.IsAny<string>()));
            var persistor = new BatchPersistor("test", args, fileMock.Object);
            var pi = new PrivateObject(persistor);
            var accessor = new BatchPersistor_Accessor(pi);

            Assert.AreEqual("Batch", accessor._xElement.Name);

        }

        [TestMethod]
        public void BatchPersistor_Constructor_VerifyXmlOutputFileh()
        {
            var args = new CCArguments();
            args.CompileOption = "SIMPLE_OPTIMIZATIONS";
            args.CompiledFileName = @"c:\output.js";
            args.Files.Add(@"c:\input.js");
            var fileMock = new Mock<IFileProvider>();
            fileMock.Setup(f => f.SaveToFile(It.IsAny<string>(), It.IsAny<string>()));
            var persistor = new BatchPersistor("test", args, fileMock.Object);
            var pi = new PrivateObject(persistor);
            var accessor = new BatchPersistor_Accessor(pi);

            Assert.AreEqual(@"c:\output.js", accessor._xElement.Attribute("outputFile").Value);

        }

        [TestMethod]
        public void BatchPersistor_Constructor_VerifyXmlCompileOption()
        {
            var args = new CCArguments();
            args.CompileOption = "SIMPLE_OPTIMIZATIONS";
            args.CompiledFileName = @"c:\output.js";
            args.Files.Add(@"c:\input.js");
            var fileMock = new Mock<IFileProvider>();
            fileMock.Setup(f => f.SaveToFile(It.IsAny<string>(), It.IsAny<string>()));
            var persistor = new BatchPersistor("test", args, fileMock.Object);
            var pi = new PrivateObject(persistor);
            var accessor = new BatchPersistor_Accessor(pi);

            Assert.AreEqual("SIMPLE_OPTIMIZATIONS", accessor._xElement.Attribute("compileOption").Value);

        }

        [TestMethod]
        public void BatchPersistor_Constructor_VerifyXmlCompileSupplier()
        {
            var args = new CCArguments();
            args.CompileOption = "SIMPLE_OPTIMIZATIONS";
            args.CompiledFileName = @"c:\output.js";
            args.Files.Add(@"c:\input.js");
            var fileMock = new Mock<IFileProvider>();
            fileMock.Setup(f => f.SaveToFile(It.IsAny<string>(), It.IsAny<string>()));
            var persistor = new BatchPersistor("test", args, fileMock.Object);
            var pi = new PrivateObject(persistor);
            var accessor = new BatchPersistor_Accessor(pi);

            Assert.AreEqual("ClosureCompilerLocal", accessor._xElement.Attribute("compileSupplier").Value);

        }

        [TestMethod]
        public void BatchPersistor_Constructor_VerifyXmlFilesCountIs1()
        {
            var args = new CCArguments();
            args.CompileOption = "SIMPLE_OPTIMIZATIONS";
            args.CompiledFileName = @"c:\output.js";
            args.Files.Add(@"c:\input.js");
            var fileMock = new Mock<IFileProvider>();
            fileMock.Setup(f => f.SaveToFile(It.IsAny<string>(), It.IsAny<string>()));
            var persistor = new BatchPersistor("test", args, fileMock.Object);
            var pi = new PrivateObject(persistor);
            var accessor = new BatchPersistor_Accessor(pi);

            Assert.AreEqual(1, accessor._xElement.Descendants("File").Count());

        }

        [TestMethod]
        public void BatchPersistor_Constructor_VerifyXmlFileName()
        {
            var args = new CCArguments();
            args.CompileOption = "SIMPLE_OPTIMIZATIONS";
            args.CompiledFileName = @"c:\output.js";
            args.Files.Add(@"c:\input.js");
            var fileMock = new Mock<IFileProvider>();
            fileMock.Setup(f => f.SaveToFile(It.IsAny<string>(), It.IsAny<string>()));
            var persistor = new BatchPersistor("test", args, fileMock.Object);
            var pi = new PrivateObject(persistor);
            var accessor = new BatchPersistor_Accessor(pi);

            Assert.AreEqual(@"c:\input.js", accessor._xElement.Descendants("File").First().Value);

        }
    }
}
